import java.util.Objects;

/**
 * 自己封装的数组
 *
 * @author HuRui
 * create on 2018/12/26 14:05
 */
public class Array<E> {

    /**
     * 默认初始数组的容量
     */
    private static final int defaultCapacity = 16;
    /**
     * 数组容器
     */
    private E[] elements;
    /**
     * 数组容量
     */
    private int count;

    /**
     * 用户指定初始容量的构造方法
     *
     * @param capacity
     */
    public Array(int capacity) {
        elements = (E[]) new Object[capacity];
        count = 0;
    }

    /**
     * 默认初始容量的构造方法
     */
    public Array() {
        this(defaultCapacity);
    }

    /**
     * 返回数组个数
     *
     * @return
     */
    public int size() {
        return count;
    }

    /**
     * 是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return count == 0;
    }

    /**
     * 新增元素
     *
     * @param e 元素
     * @return 元素位置（索引）
     */
    public int add(E e) {
        elementCheck(e);
        if (count == elements.length)
            resize();
        elements[count++] = e;
        return count;
    }

    private void elementCheck(E e) {
        if (e == null)
            throw new IllegalArgumentException("element can not be null !");
    }

    /**
     * 数组的扩容
     */
    private void resize() {
        int oldCapacity = elements.length;
        int newCapacity = oldCapacity >> 1 + oldCapacity;
        if (newCapacity > Integer.MAX_VALUE)
            newCapacity = Integer.MAX_VALUE;
        E[] es = (E[]) new Object[newCapacity];
        System.arraycopy(elements, 0, es, 0, oldCapacity);
        elements = es;
    }

    /**
     * 查找元素
     *
     * @param index 元素索引
     * @return 元素值
     */
    public E get(int index) {
        indexCheck(index);
        return elements[index];
    }

    private void indexCheck(int index) {
        if (count <= index || index < 0)
            throw new IndexOutOfBoundsException();
    }

    /**
     * 设置元素
     *
     * @param index
     * @param e
     * @return
     */
    public void set(int index, E e) {
        elementCheck(e);
        indexCheck(index);
        elements[index] = e;
    }

    /**
     * 删除元素
     *
     * @param index
     * @return
     */
    public E remove(int index) {
        indexCheck(index);
        E element = elements[index];
        for (int i = index + 1; i < count; i++)
            elements[i - 1] = elements[i];
        count--;
        return element;
    }

    /**
     * 是否存在
     *
     * @param e 目标元素
     * @return boolean
     */
    public boolean contain(E e) {
        for (E el : elements)
            if (Objects.equals(e, el))
                return true;
        return false;
    }

    /**
     * 查询指定二维数组（数据特性：从左至右依次递增，从上至下依次递增）是否存在target
     *
     * @param array
     * @param target
     * @return
     */
    private static boolean find(int[][] array, int target) {
        if (array == null || array.length == 0) return false;
        int rows = array.length;
        int columns = array[0].length;
        int row = 0, column = columns - 1;
        while (row < rows) {
            int tmp = array[row][column];
            if (target > tmp)
                row++;
            else if (target < tmp) {
                if (binarySearch(array[row], target, columns)) return true;
            } else
                //找到了
                return true;
        }
        return false;
    }

    private static boolean binarySearch(int[] ints, int target, int columns) {
        int left = 0, right = columns - 1;
        while (left <= right) {
            int middle = left + (right - left) >> 1;
            int tmpRow = ints[middle];
            if (tmpRow == target)
                //找到了
                return true;
            else if (tmpRow > target)
                right = middle - 1;
            else
                left = middle + 1;
        }
        return false;
    }

    public static void main(String[] args) {
        int row = 100, colunm = 100;
        int[][] array = new int[row][colunm];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < colunm; j++) {
                array[i][j] = i + j;
                System.out.print(i + j + "\t");
            }
            System.out.println();
        }
        long l = System.currentTimeMillis();
        for (int i = 0; i < 240; i++) {
            boolean b = find(array, i);
            System.out.println(i + ":" + b);
        }
        long l1 = System.currentTimeMillis();
        System.out.println("耗时：" + (l1 - l));
    }
}
